/*
 * Jamus
 * Copyright (C) 2010 Jamus Team
 *
 * Jamus is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Jamus is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Jamus.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef __BACKEND_MPD_PLUGIN
#define __BACKEND_MPD_PLUGIN

#include <boost/asio.hpp>
#include <boost/thread.hpp>
#include <log4cxx/logger.h>

#include "../Plugin.h"

namespace backend {
namespace mpd {

class Plugin : public backend::Plugin, private boost::noncopyable
{
public:
	/**
	 * Create the MPD backend plugin, connecting to the MPD server at the
	 * specified host and port.
	 *
	 * @param timeout in seconds.
	 */
	Plugin(const std::string& host, int port, int timeout);
	virtual ~Plugin();

	virtual void run();
	virtual void cancel();

	/*
	 * Playback
	 */
	virtual PlaybackStatus getPlaybackStatus();
	virtual void play();
	virtual void play(int);
	virtual void playPause();
	virtual void pause();
	virtual void stop();

	virtual void nextTrack();
	virtual void prevTrack();
	virtual int getCurrentTrack();
	virtual boost::posix_time::time_duration getElapsed();
	virtual void seek(const boost::posix_time::time_duration&);

	/*
	 * Mixer
	 */
	virtual int getVolume();
	virtual void setVolume(int vol);

	/*
	 * Music library
	 */
	virtual const TrackList& getTrackList();

	/*
	 * Current playlist
	 */
	virtual const Playlist& getPlaylist();
	virtual void addToPlaylist(const std::string& fileName);
	virtual void removeFromPlaylist(int index);
	virtual void clearPlaylist();

private:
	void fetchPlaylistFromServer();
	void fetchStatusFromServer();

	static log4cxx::LoggerPtr logger;

	bool running;

	mpd_connection* mpd;

	TrackList tracks;
	Playlist playlist;

	mpd_status* status; // Always kept up to date

	// Elapsed time of current song (if any). We need to keep track if this
	// ourselves because MPD does not notify us each second or so. Only when
	// we explicitly fetch the status.
	boost::posix_time::time_duration elapsedOnLastUpdate;
	boost::posix_time::ptime timeOfLastUpdate;

	// The following is needed for synchronization
	boost::mutex mutex;
	bool isIdle;
	bool isIdleAllowed;
	boost::condition_variable idleFinished;
	boost::condition_variable idleAllowed;

	friend class Lock;
	// Instantiate a Lock(*this) whenever a method of the interface is called
	// so it does not get in the way of the idle thread.
	class Lock
	{
	public:
		// Lock mutex, wait until idle is finished, and forbid idle mode
		Lock(Plugin&);
		// Unlock mutex, allow idle mode and notify idle thread of this
		~Lock();
	private:
		Plugin& p;
		boost::unique_lock<boost::mutex> lock;
	};
	int numLocksWaiting; // number of Locks currently waiting for idleFinished
};

} // namespace mpd
} // namespace backend

#endif
